package com.ml.doctor.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.InputFilter;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ml.doctor.R;


public class BasicEditItem extends LinearLayout {

    protected ImageView iconImageView;
    protected TextView titleTextView;
    protected EditText editText;

    public BasicEditItem(Context context) {
        this(context, null);
    }

    public BasicEditItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflateLayout(context);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BasicEditItem);
        int iconResId = array.getResourceId(R.styleable.BasicEditItem_edit_icon, 0);

        String title = array.getString(R.styleable.BasicEditItem_edit_title);
        int defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
        int titleSize = array.getDimensionPixelSize(R.styleable.BasicEditItem_edit_titleSize, defSize);
        int titleColor = array.getColor(R.styleable.BasicEditItem_edit_titleColor, 0xff13334d);

        String hint = array.getString(R.styleable.BasicEditItem_edit_hint);
        defSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15, getResources().getDisplayMetrics());
        int editSize = array.getDimensionPixelSize(R.styleable.BasicEditItem_edit_size, defSize);
        int editColor = array.getColor(R.styleable.BasicEditItem_edit_color, 0xff13334d);
        int hintColor = array.getColor(R.styleable.BasicEditItem_edit_hintColor, 0xff9dacb6);

        int maxLength = array.getInt(R.styleable.BasicEditItem_edit_length, Integer.MAX_VALUE);
        int titleMinWidth = array.getDimensionPixelSize(R.styleable.BasicEditItem_edit_titleMinWidth, Integer.MIN_VALUE);

        array.recycle();

        iconImageView = (ImageView) findViewById(R.id.s_icon);
        setIcon(iconResId);

        titleTextView = (TextView) findViewById(R.id.s_title);
        setTitle(title);
        setTitleSize(titleSize);
        setTitleColor(titleColor);

        editText = (EditText) findViewById(R.id.s_edit);
        setEditHint(hint);
        setEditSize(editSize);
        setEditColor(editColor);
        setEditHintColor(hintColor);

        setEditMaxLength(maxLength);
        setTitleMinWidth(titleMinWidth);
    }

    protected void inflateLayout(Context context) {
        inflate(context, R.layout.basic_edit_item, this);
    }

    public ImageView getIcon() {
        return iconImageView;
    }

    public void setIcon(int resId) {
        if (resId > 0) {
            iconImageView.setImageResource(resId);
        }
        iconImageView.setVisibility(resId == 0 ? GONE : VISIBLE);
    }

    public BasicEditItem setIcon(Drawable icon) {
        iconImageView.setImageDrawable(icon);
        return this;
    }

    public BasicEditItem setTitle(CharSequence title) {
        titleTextView.setText(title);
        return this;
    }

    public BasicEditItem setTitleSize(int size) {
        titleTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        return this;
    }

    public BasicEditItem setTitleMinWidth(int minWidth) {
        titleTextView.setMinWidth(minWidth);
        return this;
    }

    public BasicEditItem setTitleColor(int color) {
        titleTextView.setTextColor(color);
        return this;
    }

    public TextView getTitleView() {
        return titleTextView;
    }

    public BasicEditItem setEditHint(CharSequence hint) {
        editText.setHint(hint);
        return this;
    }

    public BasicEditItem setEditSize(int size) {
        editText.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
        return this;
    }

    public BasicEditItem setEditColor(int color) {
        editText.setTextColor(color);
        return this;
    }

    public BasicEditItem setEditHintColor(int color) {
        editText.setHintTextColor(color);
        return this;
    }

    public BasicEditItem setEditMaxLength(int length) {
        editText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(length)});
        return this;
    }

    public EditText getEditText() {
        return editText;
    }

    public String getEditString() {
        return editText.getText().toString();
    }

}
